//清除本地存储
//优惠券不可点击
//初始化就近买         闪电买
//初始化知道商品价格   闪电买
//初始化订单类型
//初始化备注
sessionStorage.clear();
sessionStorage.setItem("couponClickOn", "false");
sessionStorage.setItem("nearBuy", "0");
sessionStorage.setItem("zhiPrice", "1");
sessionStorage.setItem("OrderType", "0");
sessionStorage.setItem("Desc", "");
var wrongStr = ""; //提示字符
// 获取openId
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};
var openId = getQueryString("openId");
if (openId) {
    localStorage.setItem("openId", openId);
} else {
    //$.alert('请在微信中打开');
}
//swiper插件轮播
var mySwiper = new Swiper('.swiper-container', {
    direction: 'horizontal',
    loop: true, //轮播关闭
    autoplay: 5000, //可选选项，自动滑动
    // 如果需要分页器
    pagination: '.swiper-pagination',
});
//登录切换注册
$("#register_tab").click(function() {
    $("#login_box").hide();
    $("#register_box").show();
});
//注册切换登录
$("#login_tab").click(function() {
    $("#register_box").hide();
    $("#login_box").show();
});
//忘记密码切换登陆
$("#rlogin_tab").click(function() {
    $("#retrieve_box").hide();
    $("#login_box").show();
});
//登陆切换忘记密码
$("#forget_password").click(function() {
    $("#login_box").hide();
    $("#retrieve_box").show();
});
//限制手机号长度
function inputLimitTel() {
    var len = $(this).val().length;
    if (len > 11) {
        $(this).val($(this).val().substring(0, 11))
    }
};
$("#login_tel").on("keyup", inputLimitTel);
$("#user_tel").on("keyup", inputLimitTel);
//检查手机号
function checkPhone(tel) {
    if (tel == "") {
        wrongStr = "请输入手机号";
        $.toptip(wrongStr, 'error');
        return false;
    } else if (!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(tel))) {
        wrongStr = "请输入有效的手机号码";
        $.toptip(wrongStr, 'error');
        return false;
    } else {
        return true
    }
}
// 检查密码  
function checkPwd(pwd) {
    if (pwd == "") {
        wrongStr = "请输入密码";
        $.toptip(wrongStr, 'error');
        return false
    } else if ((/^[0-9A-Za-z]{6,20}$/.test(pwd)) == false) {
        wrongStr = "密码格式错误";
        $.toptip(wrongStr, 'error');
        return false
    } else {
        return true
    }
}

//注册发送验证码
// 2017.8.18新增图片验证码，防止恶意请求短信接口


// 更换验证码图片
$("#verifyCodeImg").click(function() {
    this.src = '../SDJS_TuiJian/VerifyCode.aspx?time=' + (new Date()).getTime();
});


// 注册时获取验证码
function getRegVerCode() {
    var phone = $("#login_tel").val();
    var verifyCode = $("#img_vercode").val();
    if (checkPhone(phone) == false) {
        return false;
    }
    $.ajax({
        type: "post", //请求方式：post，get  
        url: "/SDJS_TuiJian/SDJS_Register_User.ashx", //请求的页面  
        data: {
            "Type": "0",
            "phone": phone,
            "verifyCode": verifyCode
        }, //请求参数  
        //请求成功执行的方法，function内参数名随意，不影响  
        success: function(result) {
            if (result == "1") {
                reSendVerCode();
                $.toptip("验证码发送成功");
                return;
            }
            if (result == "2") {
                $.toptip("手机号已注册请登录");
                return;
            }
            if (result == "3") {
                $.toptip("手机号格式错误");
                return;
            }
            if (result == "10" || result == "11" || result == "12") {
                $("#verifyCodeImg").click();
                $("#img_vercode").focus();
                $.toptip("未输入验证码");
                return false;
            }
            if (result == "13") {
                $("#verifyCodeImg").click();
                $("#img_vercode").val('').focus();
                $.toptip("验证码错误");
                return false;
            }
            if (result == "4") {
                $.toptip("每个手机号每天最多发送5次验证码");
                return;
            } else {
                $.toptip("服务器繁忙请稍后再试");
                return;
            }
        },
        //请求执行异常执行的方法  
        error: function() {
            $.toptip("error");
        }
    });
};

//找回密码时获取验证码
function ajaxVerCode() {
    //根据type取tel值
    if (getCodeType == 0) {
        var userTel = $("#login_tel").val();
    } else if (getCodeType == 1) {
        var userTel = $("#ret_tel").val();
    }
    if (checkPhone(userTel) == false) {
        return false;
    } else {
        $.ajax({
            type: "post",
            url: "/interfaceDOC/firstProject/User.ashx?action=get_phoneCode",
            data: {
                "phone": userTel,
                "type": getCodeType
            },
            dataType: "json",
            success: function(data) {
                if (data.msgcode === "1") {
                    wrongStr = data.msg;
                    $.toptip(wrongStr, 'error');
                    trueVerCode = data.data.vilidCode;
                    reSendVerCode();
                } else {
                    wrongStr = data.msg;
                    $.toptip(wrongStr, 'error');
                    return false
                }
            },
            error: function() {
                return false
            }
        })
    }
};
var trueVerCode = ""; //接口发送的验证码
var getCodeType = 0; //验证码类型  
// 禁用发送验证码按钮，60s后重新发送 
function reSendVerCode() {
    var time = 60;
    $(".get_vercode").attr("disabled", "disabled");
    var timer = setInterval(waitTime, 1000);

    function waitTime() {
        if (time >= 0) {
            $(".get_vercode").html(time + "秒后重试");
            time--;
        } else {
            clearInterval(timer);
            $(".get_vercode").removeAttr("disabled");
            $(".get_vercode").html("获取验证码");
            time = 60;
        }
    }
}
//检查验证码
function checkVercode(vercode) {
    if (vercode == "") {
        wrongStr = "请输入验证码";
        $.toptip(wrongStr, 'error');
        return false
    } else if (vercode != trueVerCode) {
        $.toptip(wrongStr, 'error');
        return false
    } else if (vercode === trueVerCode) {
        return true
    }
}

// 图片验证码验证成功后发送验证码
$("#register_box .get_vercode").click(function() {
    getCodeType = 0;
    // console.log("注册验证码")
    getRegVerCode();
});
// 找回密码发送验证码
$("#retrieve_box .get_vercode").click(function() {
    getCodeType = 1;
    // console.log("找回密码验证码")
    ajaxVerCode();
});

//注册按钮点击
$("#register").click(function() {
    var userTel = $("#login_tel").val();
    var userPwd = $("#set_pwd").val();
    var inputVerCode = $("#input_vercode").val();
    if (checkPhone(userTel) == false) {
        return false
    } else if (inputVerCode == "") {
        wrongStr = "请输入验证码";
        $.toptip(wrongStr, 'error');
        return false
    } else if (checkPwd(userPwd) == false) {
        return false
    } else {
        tuijianReg(userTel, userPwd, inputVerCode)
    }
});
// 登陆请求
function userLogin(userTel, userPwd) {
    $.ajax({
        type: "post",
        url: "/interfaceDOC/firstProject/User.ashx?action=user_login",
        data: {
            "Phone": userTel,
            "pwd": userPwd
        },
        dataType: "json",
        success: function(data) {
            if (data.msgcode === "1") {
                //console.log(data);
                var user_info = data;
                var userInfo = JSON.stringify(user_info);
                localStorage.removeItem("tel"); //清除之前记录的电话号
                localStorage.setItem("tel", userTel); //用于标记登陆信息
                localStorage.removeItem("userInfo");
                localStorage.setItem("userInfo", userInfo);
                $('.mask').css('display', 'none');
                $("#login_box").css('display', 'none');
                //$.toast("登陆成功");
            } else {
                wrongStr = data.msg;
                $.toptip(wrongStr, 'error');
                return false
            }
        },
        error: function() {
            return false
        }
    })
};
// 推荐分享界面用户注册
function tuijianReg(phone, Pwd, voidCode) {
    $.ajax({
        type: "post", //请求方式：post，get  
        url: "/SDJS_TuiJian/SDJS_Register_User.ashx", //请求的页面  
        data: {
            "Type": "1",
            "phone": phone,
            "Pwd": Pwd,
            "voidCode": voidCode
        }, //请求参数  
        //请求成功执行的方法，function内参数名随意，不影响  
        success: function(result) {
            console.log(result)
            if (result == "1") {
                $.toptip("手机号格式错误");
                return;
            }
            if (result == "2") {
                $.toptip("验证码错误");
                return;
            }
            if (result == "3") {
                $.toptip("用户已存在");
                return;
            }
            if (result == "4") {
                $.toptip("注册成功", 1000, 'error');
                userLogin(phone, Pwd);
                $("#register_box").hide();
                return;
            }
            if (result == "0") {
                $.toptip("注册失败");
                return;
            }
            if (result == "10") {
                $.toptip("type或id为空");
                return;
            } else {
                $.toptip("服务器繁忙请稍后再试");
                return;
            }
        },
        //请求执行异常执行的方法  
        error: function() {
            $.toptip("error");
        }
    });
}

// 注册请求-----软盟接口
function userRegister(userTel, userPwd) {
    $.ajax({
        type: "post",
        url: "/interfaceDOC/firstProject/User.ashx?action=user_register",
        data: {
            "Phone": userTel,
            "Pwd": userPwd,
            "CityId": "155"
        },
        dataType: "json",
        success: function(data) {
            if (data.msgcode === "1") {
                // 注册成功后调用登陆接口 2017.7.19
                userLogin(userTel, userPwd);
                $("#register_box").hide();
                //注册成功后推送优惠券提示   2017.7.17 GJC
                //先提醒注册成功
                //后弹出注册成功推送
                $.toptip(data.msg, 1000, 'error');
                // function loginPush(){
                //     $.notification({
                //       text: "您有3张新用户注册优惠券已到账，快来查看吧",
                //       time:5000,
                //       onClick: function() {
                //         window.location.href="html/coupon.html"
                //       }
                //     })
                // }
                // setTimeout(loginPush,1200)
            } else {
                wrongStr = data.msg;
                $.toptip(wrongStr, 'error');
                return false
            }
        },
        error: function() {
            return false
        }
    })
}
//登陆验证
$('#user_login').click(function() {
    var userTel = $("#user_tel").val();
    var userPwd = $("#user_pwd").val();
    if (checkPhone(userTel) == false) {
        return false
    } else if (checkPwd(userPwd) == false) {
        return false
    } else {
        userLogin(userTel, userPwd);
    }
});
//找回密码
$("#retrieve_submit").click(function() {
    var userTel = $("#ret_tel").val();
    var userPwd = $("#ret_pwd").val();
    var inputVerCode = $("#ret_vercode").val();
    console.log(userTel)
    if (checkPhone(userTel) == false) {
        return false
    } else if (checkVercode(inputVerCode) == false) {
        wrongStr = "验证码错误";
        $.toptip(wrongStr, 'error');
        return false
    } else if (checkPwd(userPwd) == false) {
        return false
    } else {
        $.ajax({
            type: "post",
            url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_Pwd",
            data: {
                "Phone": userTel,
                "NewPwd": userPwd,
            },
            dataType: "json",
            success: function(data) {
                if (data.msgcode === "1") {
                    wrongStr = data.msg;
                    $.toptip(wrongStr, 'error');
                    $("#login_box").show();
                    $("#retrieve_box").hide();
                } else {
                    wrongStr = data.msg;
                    $.toptip(wrongStr, 'error');
                    return false
                }
            },
            error: function() {
                return false
            }
        })
    }
})

//判断是否登陆，否则a链接跳出登录层
$('.sdjs_index a').click(function() {
    if (localStorage.getItem("tel") == null) {
        $('.mask').css('display', 'block'); //显示登录页
        $("#login_box").css("display", "block");
        return false;
    }
});
//退出登陆注册
$(".login_box .close").click(function() {
    $(".login_box").hide();
    $(".mask").hide();
});